package leetcode.easy;

import java.util.PriorityQueue;

/**
 * ClassName: $1046_LastStoneWeight
 * Description:
 * date: 2022-01-27 13:01
 *
 * @author liyifan
 */
public class $1046_LastStoneWeight {
    public int lastStoneWeight(int[] stones) {
        PriorityQueue<Integer> heap = new PriorityQueue<>((x, y)->{
            return Integer.compare(y, x);
        });
        for(int i = 0;i < stones.length;i++) {
            heap.offer(stones[i]);
        }
        while (!heap.isEmpty()) {
            if (heap.size() == 1) {
                // 只剩最后一个
                return heap.poll();
            } else {
                int heavy = heap.poll();
                int light = heap.poll();
                heap.offer(heavy - light);
            }
        }
        return 0;
    }
}
